import {
  getPageList,
  addConfigGroup,
  deleteConfigGroup,
  updateConfigGroup,
  pageCount,
} from '@/services/configGroupApi';

export default {
  namespace: 'configGroup', // 这个是标示当前model的

  // 下面是定义的数据模型
  state: {
    tableList: [],
    tableLoading: false,
    searchParam: {},
    totalNumber: 0,
    pageNo: 1,
  },

  // 异步处理函数
  effects: {
    // 获取配置列表
    *getPageList({ payload }, { call, put }) {
      console.log('configGroup.getPageList 参数：');
      console.log(JSON.stringify(payload));

      const values = {
        ...payload.searchParam,
        pager: payload.pager,
      };

      console.log(JSON.stringify(values));

      const response = yield call(getPageList, values);
      yield put({
        type: 'handlePageListResult',
        payload: {
          response,
          searchParam: payload.searchParam,
          pageNo: payload.pager.pageNo,
        },
      });
    },

    *getListCount({ payload }, { call, put }) {
      console.log('configGroup.getListCount 参数：');
      console.log(JSON.stringify(payload));

      const params =
        payload === undefined || payload.searchParam === undefined ? {} : payload.searchParam;
      const pager = payload === undefined || payload.pager === undefined ? {} : payload.pager;
      const values = {
        ...params,
        ...pager,
      };

      console.log(JSON.stringify(values));
      const count = yield call(pageCount, values);
      yield put({
        type: 'handleCountResult',
        payload: count,
      });
    },

    // 增加组配置
    *add({ payload }, { call, put }) {
      console.log('configGroup.add 参数：');
      console.log(JSON.stringify(payload));
      const response = yield call(addConfigGroup, payload);
      yield put({
        type: 'handleAddResult',
        payload: response,
      });
    },

    // 删除组配置
    *delete({ payload }, { call, put }) {
      console.log('configGroup.delete 参数：');
      console.log(JSON.stringify(payload));
      const response = yield call(deleteConfigGroup, payload);
      yield put({
        type: 'handleDeleteResult',
        payload: {
          response,
          id: payload,
        },
      });
    },

    // 修改组配置
    *update({ payload }, { call, put }) {
      console.log('configGroup.update 参数：');
      console.log(JSON.stringify(payload));
      const response = yield call(updateConfigGroup, payload);
      yield put({
        type: 'handleUpdateResult',
        payload: {
          response,
          param: payload,
        },
      });
      return {
        tableLoading: true,
      };
    },
  },

  reducers: {
    setSearchParam(state, action) {
      return {
        ...state,
        searchParam: action,
      };
    },
    setTableLoading(state) {
      return {
        ...state,
        tableLoading: true,
      };
    },

    handleCountResult(state, action) {
      console.log('configGroup.handleCountResult 返回的结果');
      console.log(JSON.stringify(action.payload));
      return {
        ...state,
        totalNumber: action.payload,
      };
    },

    handlePageListResult(state, action) {
      console.log('configGroup.handlePageListResult 返回的结果');
      const pl = action.payload;

      console.log(JSON.stringify(action));
      return {
        ...state,
        searchParam: pl.searchParam,
        pageNo: pl.pageNo,
        tableList: pl.response,
        tableLoading: false,
      };
    },

    handleAddResult(state) {
      return {
        ...state,
      };
    },

    handleUpdateResult(state, action) {
      console.log('configGroup.handleUpdateResult 返回的结果');
      console.log(JSON.stringify(action.payload));

      // 若成功，则不不需要重新加载后端，而是直接修改前段的内存数据
      const { tableList } = state;
      const tableListNew = tableList;
      const newItem = action.payload.param;
      const index = tableListNew.findIndex(item => newItem.id === item.id);

      if (action.payload.response === '1') {
        if (index > -1) {
          const item = tableListNew[index];
          tableListNew.splice(index, 1, {
            ...item,
            ...newItem,
          });
        }
      }
      return {
        ...state,
        tableList: tableListNew,
        tableLoading: false,
      };
    },

    handleDeleteResult(state, action) {
      console.log('configGroup.handleDeleteResult 返回的结果');
      console.log(action.payload);
      const { tableList, tableLoading } = state;

      let tableListNew = tableList;
      let tableLoadingNew = tableLoading;
      if (action.payload.response === '1') {
        // 若成功，则不不需要重新加载后端，而是直接删除前端的内存数据
        tableListNew = tableList.filter(item => item.id !== action.payload.id);
        tableLoadingNew = false;
      }
      return {
        ...state,
        tableList: tableListNew,
        tableLoading: tableLoadingNew,
      };
    },
  },
};
